﻿using HslCommunication.Enthernet.Redis;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SYSoft.Comm;
using SYSoft.Comm.Utils;
using SYSoft.Model;
using SYSoft.Model.ViewModels.DeviceData;
using SYSoft.Model.ViewModels.DeviceManage.HomePage;
using SYSoft.Model.ViewModels.DeviceManage.YouqiPage;
using SYSoft.Model.ViewModels.HomePage;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace SYSoft.Controllers.DeviceManage
{
    public class HomePageController : BaseController
    {
        public HomePageController()
        {

        }

        /// <summary>
        /// 获取首页图片显示   使用base64 存在数据库
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public MessageModel GetHomeImg()
        {
            ImageShowVM imagevm = new ImageShowVM()
            {
                dengliziURL = Directory.GetCurrentDirectory() + "/wwwroot/等离子.jpg",
                fanzhaunjiURL = Directory.GetCurrentDirectory() + "/wwwroot/翻转机.jpg",
                jiqirenURL = Directory.GetCurrentDirectory() + "/wwwroot/机器人.jpg",
                jianzhexianURL = Directory.GetCurrentDirectory() + "/wwwroot/剪折线.jpg",
                maihuhanURL = Directory.GetCurrentDirectory() + "/wwwroot/埋弧焊.jpg",
                paowanjiURL = Directory.GetCurrentDirectory() + "/wwwroot/抛丸机.jpg",
                youqixianURL = Directory.GetCurrentDirectory() + "/wwwroot/油漆线.jpg",
                yugongURL = Directory.GetCurrentDirectory() + "/wwwroot/预拱.jpg",
                zidonghanURL = Directory.GetCurrentDirectory() + "/wwwroot/自动焊.jpg",
                zuhanURL = Directory.GetCurrentDirectory() + "/wwwroot/组焊.jpg",
            };
            return MessageModel.CreateSuccessResult(imagevm);
        }

        /// <summary>
        /// 获取首页实时数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public MessageModel DeviceRealStatusNew()
        {
           var res = NetFactory.redisPool.ReadKey("MTCD:STAT:HomeDeviceStatus");
            if (res.IsSuccess)
            {
                HomeRealData homeRealData = JsonConvert.DeserializeObject<HomeRealData>(res.Content);
                return MessageModel.CreateSuccessResult(homeRealData);
            }
            return new MessageModel(res.Message);
        }

        /// <summary>
        /// 获取首页实时数据(废除） 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public MessageModel DeviceRealStatus()
        {
            HomeRealData homeRealData = new HomeRealData();
            int denglizi_alarmCount = 0;
            int maihuhan_alarmCount = 0;
            int yugong_alarmCount = 0;
            int zidonghan_alarmCount = 0;
            int jianzhexian_alarmCount = 0;
            int jiqiren_alarmCount = 0;
            int youqixian_alarmCount = 0;
            int paowanji_alarmCount = 0;
            int fanzhuanji_alarmCount = 0;
            int zuhan_alarmCount = 0;
            try
            {
                //等离子
                var denglizi1 = NetFactory.redisPool.ReadHashKey("MTCD:EnergyGateWay", DeviceID.等离子.ToString());
                if (denglizi1.IsSuccess)
                {
                    JArray obj = JArray.Parse(denglizi1.Content);
                    if (obj[0].Value<bool>("IsActive"))
                    {
                        homeRealData.DengliziEnergy = obj[0].Value<float>("EPI_power"); //总能耗
                        if (obj[0].Value<float>("CurrentA") < ServerInfo.RunStatusCurrent)
                        {
                            homeRealData.DengliziStatus = Convert.ToInt32(HomeDeviceStatus.停机);
                        }
                        else
                        {
                            homeRealData.DengliziStatus = Convert.ToInt32(HomeDeviceStatus.运行中);
                        }
                        JArray energyarray = JArray.Parse(obj[0].Value<JArray>("Alarm").ToString());
                        bool[] alarm = energyarray.ToObject<bool[]>();
                        for (int i = 0; i < alarm.Length; i++)
                        {
                            if (alarm[i])
                                denglizi_alarmCount++;
                        }
                    }
                }
                else
                {
                    return new MessageModel(denglizi1.Message);
                }
                var denglizi2 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.等离子.ToString());
                if (denglizi2.IsSuccess)
                {
                    JObject obj = JObject.Parse(denglizi2.Content);
                    if (obj.Value<bool>("IsActive"))
                    {
                        homeRealData.Denglizichanlaing = Convert.ToInt32(obj["DailyOutput"]); //产量
                        JArray passarray = JArray.Parse(obj.Value<JArray>("Alarm").ToString());
                        bool[] alarm = passarray.ToObject<bool[]>();
                        for (int i = 0; i < alarm.Length; i++)
                        {
                            if (alarm[i])
                                denglizi_alarmCount++;
                        }
                    }
                    else
                    {
                        homeRealData.DengliziStatus = Convert.ToInt32(HomeDeviceStatus.离线);
                    }
                }
                if (denglizi_alarmCount > 0)
                {
                    homeRealData.DengliziStatus = Convert.ToInt32(HomeDeviceStatus.故障);
                }

                // 埋弧焊
                var maihuhan1 = NetFactory.redisPool.ReadHashKey("MTCD:EnergyGateWay", DeviceID.埋弧焊.ToString());
                if (maihuhan1.IsSuccess)
                {
                    JArray obj = JArray.Parse(maihuhan1.Content);
                    if (obj[0].Value<bool>("IsActive"))
                    {
                        homeRealData.MaihuhanEnergy = obj[0].Value<float>("EPI_power"); //总能耗
                        if (obj[0].Value<float>("CurrentA") < ServerInfo.RunStatusCurrent)
                        {
                            homeRealData.MaihuhanStatus = Convert.ToInt32(HomeDeviceStatus.停机);
                        }
                        else
                        {
                            homeRealData.MaihuhanStatus = Convert.ToInt32(HomeDeviceStatus.运行中);
                        }
                        JArray energyarray = JArray.Parse(obj[0].Value<JArray>("Alarm").ToString());
                        bool[] alarm = energyarray.ToObject<bool[]>();
                        for (int i = 0; i < alarm.Length; i++)
                        {
                            if (alarm[i])
                                maihuhan_alarmCount++;
                        }
                    }
                }
                var maihuhan2 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.埋弧焊.ToString());
                if (maihuhan2.IsSuccess)
                {
                    JObject obj = JObject.Parse(maihuhan2.Content);
                    if (obj.Value<bool>("IsActive"))
                    {
                        homeRealData.Maihuhanchanlaing = Convert.ToInt32(obj["DailyOutput"]); //产量
                        JArray passarray = JArray.Parse(obj.Value<JArray>("Alarm").ToString());
                        bool[] alarm = passarray.ToObject<bool[]>();
                        for (int i = 0; i < alarm.Length; i++)
                        {
                            if (alarm[i])
                                maihuhan_alarmCount++;
                        }
                    }
                    else
                    {
                        homeRealData.MaihuhanStatus = Convert.ToInt32(HomeDeviceStatus.离线);
                    }
                }
                if (maihuhan_alarmCount > 0)
                {
                    homeRealData.MaihuhanStatus = Convert.ToInt32(HomeDeviceStatus.故障);
                }


             

                // 自动焊   预拱   共用
                var zidonghan1 = NetFactory.redisPool.ReadHashKey("MTCD:EnergyGateWay", DeviceID.自动焊.ToString());
                if (zidonghan1.IsSuccess)
                {
                    JArray obj = JArray.Parse(zidonghan1.Content);
                    if (obj[0].Value<bool>("IsActive"))
                    {
                        homeRealData.YugongEnergy = obj[0].Value<float>("EPI_power")*0.05f; //总能耗
                        homeRealData.zidonghanEnergy = obj[0].Value<float>("EPI_power") * 0.95f; //总能耗
                        if (obj[0].Value<float>("CurrentA") < ServerInfo.RunStatusCurrent)
                        {
                            homeRealData.zidonghanStatus = Convert.ToInt32(HomeDeviceStatus.停机);
                            homeRealData.YugongStatus = Convert.ToInt32(HomeDeviceStatus.停机);
                        }
                        else
                        {
                            homeRealData.zidonghanStatus = Convert.ToInt32(HomeDeviceStatus.运行中);
                            homeRealData.YugongStatus = Convert.ToInt32(HomeDeviceStatus.运行中);
                        }
                        JArray energyarray = JArray.Parse(obj[0].Value<JArray>("Alarm").ToString());
                        bool[] alarm = energyarray.ToObject<bool[]>();
                        for (int i = 0; i < alarm.Length; i++)
                        {
                            if (alarm[i])
                                zidonghan_alarmCount++;
                        }
                    }
                }
                var zidonghan2 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.自动焊.ToString());
                if (zidonghan2.IsSuccess)
                {
                    JObject obj = JObject.Parse(zidonghan2.Content);
                    if (obj.Value<bool>("IsActive"))
                    {
                        homeRealData.zidonghanchanlaing = Convert.ToInt32(obj["DailyOutput"]); //产量
                        JArray passarray = JArray.Parse(obj.Value<JArray>("Alarm").ToString());
                        bool[] alarm = passarray.ToObject<bool[]>();
                        for (int i = 0; i < alarm.Length; i++)
                        {
                            if (alarm[i])
                                zidonghan_alarmCount++;
                        }
                    }
                    else
                    {
                        homeRealData.zidonghanStatus = Convert.ToInt32(HomeDeviceStatus.离线);
                    }
                }
                if (zidonghan_alarmCount > 0)
                {
                    homeRealData.zidonghanStatus = Convert.ToInt32(HomeDeviceStatus.故障);
                }



                // 预拱
                //var yugong1 = redisClient.ReadHashKey("MTCD:EnergyGateWay", DeviceID.预拱.ToString());
                //if (yugong1.IsSuccess)
                //{
                //    JArray obj = JArray.Parse(yugong1.Content);
                //    if (obj[0].Value<bool>("IsActive"))
                //    {
                //        homeRealData.YugongEnergy = obj[0].Value<float>("EPI_power"); //总能耗
                //        if (obj[0].Value<float>("CurrentA") < ServerInfo.RunStatusCurrent)
                //        {
                //            homeRealData.YugongStatus = Convert.ToInt32(HomeDeviceStatus.停机);
                //        }
                //        else
                //        {
                //            homeRealData.YugongStatus = Convert.ToInt32(HomeDeviceStatus.运行中);
                //        }
                //        JArray energyarray = JArray.Parse(obj[0].Value<JArray>("Alarm").ToString());
                //        bool[] alarm = energyarray.ToObject<bool[]>();
                //        for (int i = 0; i < alarm.Length; i++)
                //        {
                //            if (alarm[i])
                //                yugong_alarmCount++;
                //        }
                //    }
                //}

                var yugong2 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.预拱.ToString());
                if (yugong2.IsSuccess)
                {
                    JObject obj = JObject.Parse(yugong2.Content);
                    if (obj.Value<bool>("IsActive"))
                    {
                        homeRealData.Yugongchanlaing = Convert.ToInt32(obj["DailyOutput"]); //产量
                        JArray passarray = JArray.Parse(obj.Value<JArray>("Alarm").ToString());
                        bool[] alarm = passarray.ToObject<bool[]>();
                        for (int i = 0; i < alarm.Length; i++)
                        {
                            if (alarm[i])
                                yugong_alarmCount++;
                        }
                    }
                    else
                    {
                        homeRealData.YugongStatus = Convert.ToInt32(HomeDeviceStatus.离线);
                    }
                }
                if (yugong_alarmCount > 0)
                {
                    homeRealData.YugongStatus = Convert.ToInt32(HomeDeviceStatus.故障);
                }



                // 剪折线
                var jianzhexian1 = NetFactory.redisPool.ReadHashKey("MTCD:EnergyGateWay", DeviceID.剪折线.ToString());
                if (jianzhexian1.IsSuccess)
                {
                    JArray obj = JArray.Parse(jianzhexian1.Content);
                    if (obj[0].Value<bool>("IsActive"))
                    {
                        homeRealData.JianzhexianEnergy = obj[0].Value<float>("EPI_power"); //总能耗
                        if (obj[0].Value<float>("CurrentA") < ServerInfo.RunStatusCurrent)
                        {
                            homeRealData.JianzhexianStatus = Convert.ToInt32(HomeDeviceStatus.停机);
                        }
                        else
                        {
                            homeRealData.JianzhexianStatus = Convert.ToInt32(HomeDeviceStatus.运行中);
                        }
                        JArray energyarray = JArray.Parse(obj[0].Value<JArray>("Alarm").ToString());
                        bool[] alarm = energyarray.ToObject<bool[]>();
                        for (int i = 0; i < alarm.Length; i++)
                        {
                            if (alarm[i])
                                jianzhexian_alarmCount++;
                        }
                    }
                }
                var jianzhexian2 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.剪折线.ToString());
                if (jianzhexian2.IsSuccess)
                {
                    JObject obj = JObject.Parse(jianzhexian2.Content);
                    if (obj.Value<bool>("IsActive"))
                    {
                        homeRealData.Jianzhexianchanlaing = Convert.ToInt32(obj["DailyOutput"]); //产量
                        JArray passarray = JArray.Parse(obj.Value<JArray>("Alarm").ToString());
                        bool[] alarm = passarray.ToObject<bool[]>();
                        for (int i = 0; i < alarm.Length; i++)
                        {
                            if (alarm[i])
                                jianzhexian_alarmCount++;
                        }
                    }
                    else
                    {
                        homeRealData.JianzhexianStatus = Convert.ToInt32(HomeDeviceStatus.离线);
                    }
                }
                if (jianzhexian_alarmCount > 0)
                {
                    homeRealData.JianzhexianStatus = Convert.ToInt32(HomeDeviceStatus.故障);
                }


                // 机器人
                var jiqiren1 = NetFactory.redisPool.ReadHashKey("MTCD:EnergyGateWay", DeviceID.机器人.ToString());
                if (jiqiren1.IsSuccess)
                {
                    JArray obj = JArray.Parse(jiqiren1.Content);
                    if (obj[0].Value<bool>("IsActive"))
                    {
                        homeRealData.JiqirenEnergy = obj[0].Value<float>("EPI_power"); //总能耗
                        if (obj[0].Value<float>("CurrentA") < ServerInfo.RunStatusCurrent)
                        {
                            homeRealData.JiqirenStatus = Convert.ToInt32(HomeDeviceStatus.停机);
                        }
                        else
                        {
                            homeRealData.JiqirenStatus = Convert.ToInt32(HomeDeviceStatus.运行中);
                        }
                        JArray energyarray = JArray.Parse(obj[0].Value<JArray>("Alarm").ToString());
                        bool[] alarm = energyarray.ToObject<bool[]>();
                        for (int i = 0; i < alarm.Length; i++)
                        {
                            if (alarm[i])
                                jiqiren_alarmCount++;
                        }
                    }
                }
                var jiqiren2 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.机器人.ToString());
                if (jiqiren2.IsSuccess)
                {
                    JObject obj = JObject.Parse(jiqiren2.Content);
                    if (obj.Value<bool>("IsActive"))
                    {
                        homeRealData.Jiqirenchanlaing = Convert.ToInt32(obj["DailyOutput"]); //产量
                        JArray passarray = JArray.Parse(obj.Value<JArray>("Alarm").ToString());
                        bool[] alarm = passarray.ToObject<bool[]>();
                        for (int i = 0; i < alarm.Length; i++)
                        {
                            if (alarm[i])
                                jiqiren_alarmCount++;
                        }
                    }
                    else
                    {
                        homeRealData.JiqirenStatus = Convert.ToInt32(HomeDeviceStatus.离线);
                    }
                }
                if (jiqiren_alarmCount > 0)
                {
                    homeRealData.JiqirenStatus = Convert.ToInt32(HomeDeviceStatus.故障);
                }

                // 抛丸机
                var paowanji1 = NetFactory.redisPool.ReadHashKey("MTCD:EnergyGateWay", DeviceID.抛丸机.ToString());
                if (paowanji1.IsSuccess)
                {
                    JArray obj = JArray.Parse(paowanji1.Content);
                    if (obj[0].Value<bool>("IsActive"))
                    {
                        homeRealData.PaowanjiEnergy = obj[0].Value<float>("EPI_power"); //总能耗
                        if (obj[0].Value<float>("CurrentA") < ServerInfo.RunStatusCurrent)
                        {
                            homeRealData.PaowanjiStatus = Convert.ToInt32(HomeDeviceStatus.停机);
                        }
                        else
                        {
                            homeRealData.PaowanjiStatus = Convert.ToInt32(HomeDeviceStatus.运行中);
                        }
                        JArray energyarray = JArray.Parse(obj[0].Value<JArray>("Alarm").ToString());
                        bool[] alarm = energyarray.ToObject<bool[]>();
                        for (int i = 0; i < alarm.Length; i++)
                        {
                            if (alarm[i])
                                paowanji_alarmCount++;
                        }
                    }
                }
                var paowanji2 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.抛丸机.ToString());
                if (paowanji2.IsSuccess)
                {
                    JObject obj = JObject.Parse(paowanji2.Content);
                    if (obj.Value<bool>("IsActive"))
                    {
                        homeRealData.Paowanjichanlaing = Convert.ToInt32(obj["DailyOutput"]); //产量
                        JArray passarray = JArray.Parse(obj.Value<JArray>("Alarm").ToString());
                        bool[] alarm = passarray.ToObject<bool[]>();
                        for (int i = 0; i < alarm.Length; i++)
                        {
                            if (alarm[i])
                                paowanji_alarmCount++;
                        }
                    }
                    else
                    {
                        homeRealData.PaowanjiStatus = Convert.ToInt32(HomeDeviceStatus.离线);
                    }
                }
                if (paowanji_alarmCount > 0)
                {
                    homeRealData.PaowanjiStatus = Convert.ToInt32(HomeDeviceStatus.故障);
                }


                //油漆线
                var youqi1 = NetFactory.redisPool.ReadHashKey("MTCD:EnergyGateWay", DeviceID.油漆线.ToString());
                if (youqi1.IsSuccess)
                {
                    JArray obj = JArray.Parse(youqi1.Content);
                    for (int i = 0; i < obj.Count; i++)
                    {
                        JObject item1 = (JObject)obj[i];
                        if (item1.Value<bool>("IsActive"))
                        {
                            homeRealData.youqixianEnergy = homeRealData.youqixianEnergy + item1.Value<float>("EPI_power");

                            if (item1.Value<float>("CurrentA") < ServerInfo.RunStatusCurrent)
                            {
                                homeRealData.youqixianStatus = Convert.ToInt32(HomeDeviceStatus.停机);
                            }
                            else
                            {
                                homeRealData.youqixianStatus = Convert.ToInt32(HomeDeviceStatus.运行中);
                            }
                            JArray energyarray = JArray.Parse(item1.Value<JArray>("Alarm").ToString());
                            bool[] alarm = energyarray.ToObject<bool[]>();
                            for (int j  = 0; j < alarm.Length; j++)
                            {
                                if (alarm[i])
                                    youqixian_alarmCount++;
                            }
                        }
                    }
                }
                var youqi2 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.油漆线.ToString());
                if (youqi2.IsSuccess)
                {
                    JObject obj = JObject.Parse(youqi2.Content);
                    if (obj.Value<bool>("IsActive"))
                    {
                        homeRealData.youqixianchanlaing = Convert.ToInt32(obj["DailyOutput"]); //产量
                        JArray passarray = JArray.Parse(obj.Value<JArray>("Alarm").ToString());
                        bool[] alarm = passarray.ToObject<bool[]>();
                        for (int i = 0; i < alarm.Length; i++)
                        {
                            if (alarm[i])
                                youqixian_alarmCount++;
                        }
                    }
                    else
                    {
                        homeRealData.youqixianStatus = Convert.ToInt32(HomeDeviceStatus.离线);
                    }
                }
                if (youqixian_alarmCount > 0)
                {
                    homeRealData.youqixianStatus = Convert.ToInt32(HomeDeviceStatus.故障);
                }



                //组焊 
                var zuhan1 = NetFactory.redisPool.ReadHashKey("MTCD:EnergyGateWay", DeviceID.组焊.ToString());
                if (zuhan1.IsSuccess)
                {
                    JArray obj = JArray.Parse(zuhan1.Content);
                    for (int i = 0; i < obj.Count; i++)
                    {
                        JObject item1 = (JObject)obj[i];
                        if (item1.Value<bool>("IsActive"))
                        {
                            if(i<3) // AP13+14+15
                            {
                                homeRealData.ZuhanEnergy = homeRealData.ZuhanEnergy + item1.Value<float>("EPI_power");
                                if (item1.Value<float>("CurrentA") < ServerInfo.RunStatusCurrent)
                                {
                                    homeRealData.ZuhanStatus = Convert.ToInt32(HomeDeviceStatus.停机);
                                }
                                else
                                {
                                    homeRealData.ZuhanStatus = Convert.ToInt32(HomeDeviceStatus.运行中);
                                }
                                JArray energyarray = JArray.Parse(item1.Value<JArray>("Alarm").ToString());
                                bool[] alarm = energyarray.ToObject<bool[]>();
                                for (int j = 0; j < alarm.Length; j++)
                                {
                                    if (alarm[i])
                                        zuhan_alarmCount++;
                                }
                            }
                            else //翻转
                            {
                                homeRealData.FanzhuanjiEnergy = item1.Value<float>("EPI_power");
                                if (item1.Value<float>("CurrentA") < ServerInfo.RunStatusCurrent)
                                {
                                    homeRealData.FanzhuanjiStatus = Convert.ToInt32(HomeDeviceStatus.停机);
                                }
                                else
                                {
                                    homeRealData.FanzhuanjiStatus = Convert.ToInt32(HomeDeviceStatus.运行中);
                                }

                                JArray energyarray = JArray.Parse(item1.Value<JArray>("Alarm").ToString());
                                bool[] alarm = energyarray.ToObject<bool[]>();
                                for (int j = 0; j < alarm.Length; j++)
                                {
                                    if (alarm[i])
                                        fanzhuanji_alarmCount++;
                                }
                            }
                            
                        }
                    }
                }
                var zuhan2 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.组焊.ToString());
                if (zuhan2.IsSuccess)
                {
                    JObject obj = JObject.Parse(zuhan2.Content);
                    if (obj.Value<bool>("IsActive"))
                    {
                        homeRealData.Zuhanchanlaing = Convert.ToInt32(obj["DailyOutput"]); //产量
                        homeRealData.Fanzhuanjichanlaing = Convert.ToInt32(obj["DailyOutput"]); //产量
                        JArray passarray = JArray.Parse(obj.Value<JArray>("Alarm").ToString());
                        bool[] alarm = passarray.ToObject<bool[]>();
                        for (int i = 0; i < alarm.Length; i++)
                        {
                            if (alarm[i])
                                fanzhuanji_alarmCount++;
                        }
                    }
                    else
                    {
                        homeRealData.ZuhanStatus = Convert.ToInt32(HomeDeviceStatus.离线);
                        homeRealData.FanzhuanjiStatus = Convert.ToInt32(HomeDeviceStatus.离线);
                    }
                }
                if (zuhan_alarmCount > 0)
                {
                    homeRealData.ZuhanStatus = Convert.ToInt32(HomeDeviceStatus.故障);
                }
                if (fanzhuanji_alarmCount > 0)
                {
                    homeRealData.FanzhuanjiStatus = Convert.ToInt32(HomeDeviceStatus.故障);
                }

                return MessageModel.CreateSuccessResult(homeRealData);
            }
            catch (Exception ex)
            {

                return new MessageModel(ex.Message);
            }
           
        }


        [HttpGet]
        public MessageModel DeviceTopRealStatus()
        {
            TopRealData realData = new TopRealData();
            try
            {
                var res1 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.空压机.ToString());
                if (res1.IsSuccess)
                {
                    JObject obj = JObject.Parse(res1.Content);
                    if (obj.Value<bool>("IsActive"))
                    {
                        //JArray array1 = JArray.Parse(obj["PassData"].ToString());
                        ////float[] data = array1.ToObject<float[]>();
                        ////realData.Air_Press1 = data[0];
                        ////realData.Air_Temp1 = data[1];
                        ////realData.Air_Press2 = data[2];
                        //realData.Air_Temp2 = data[3];

                        JArray arraydata = JArray.Parse(obj["PassData"].ToString());
                        realData.Air_Press1 =Convert.ToSingle(arraydata[0].Children().ToList()[0].ToObject<JProperty>().Value);
                        realData.Air_Temp1 = Convert.ToSingle(arraydata[1].Children().ToList()[0].ToObject<JProperty>().Value);
                        realData.Air_Press2 = Convert.ToSingle(arraydata[2].Children().ToList()[0].ToObject<JProperty>().Value);
                        realData.Air_Temp2 = Convert.ToSingle(arraydata[3].Children().ToList()[0].ToObject<JProperty>().Value);


                        JArray array = JArray.Parse(obj["Alarm"].ToString());
                        bool[] alarm = array.ToObject<bool[]>();
                        for (int i = 0; i < alarm.Length; i++)
                        {
                            if (i < 2)
                            {
                                if (alarm[i])
                                    realData.Air_Status1 = true;
                            }
                            else
                            {
                                if (alarm[i])
                                    realData.Air_Status2 = true;
                            }
                        }
                    }
                }



                var res2 = NetFactory.redisPool.ReadHashKey("MTCD:EnergyGateWay", DeviceID.高配房.ToString());
                var res2_1 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.高配房.ToString());
                if (res2.IsSuccess)
                {
                    JArray jObject1 = JArray.Parse(res2.Content);
                    if (jObject1[0].Value<bool>("IsActive"))
                    {
                        realData.Gao_CurrentA = Convert.ToSingle(jObject1[0]["CurrentA"]);
                        realData.Gao_CurrentB = Convert.ToSingle(jObject1[0]["CurrentB"]);
                        realData.Gao_CurrentC = Convert.ToSingle(jObject1[0]["CurrentC"]);
                        realData.Gao_VoltageA = Convert.ToSingle(jObject1[0]["VoltageA"]);
                        realData.Gao_VoltageB = Convert.ToSingle(jObject1[0]["VoltageB"]);
                        realData.Gao_VoltageC = Convert.ToSingle(jObject1[0]["VoltageC"]);

                        JArray array = JArray.Parse(jObject1[0]["Alarm"].ToString());
                        bool[] alarm = array.ToObject<bool[]>();
                        for (int i = 0; i < alarm.Length; i++)
                        {
                            if (alarm[0] || alarm[3])
                                realData.Gao_StatusA = true;
                            if (alarm[1] || alarm[4])
                                realData.Gao_StatusB = true;
                            if (alarm[2] || alarm[5])
                                realData.Gao_StatusC = true;
                        }
                    }
                }

                if (res2_1.IsSuccess)
                {
                    JObject obj = JObject.Parse(res2_1.Content);
                    if (obj.Value<bool>("IsActive"))
                    {
                        realData.Gao_TempA = Convert.ToSingle(obj["ATemp"]);
                        realData.Gao_TempB = Convert.ToSingle(obj["BTemp"]);
                        realData.Gao_TempC = Convert.ToSingle(obj["CTemp"]);
                    }
                }

                var res3 = NetFactory.redisPool.ReadHashKey("MTCD:ConstGateWay", DeviceID.混合气站.ToString());
                if (res3.IsSuccess)
                {
                    JObject obj = JObject.Parse(res3.Content);
                    if (obj.Value<bool>("IsActive"))
                    {
                        float actruelly = Convert.ToSingle(obj["ActruellyUsed"]);
                        realData.Hun_ARPress = Convert.ToSingle(obj["ARPress"]);
                        realData.Hun_CO2Press = Convert.ToSingle(obj["CO2Press"]);
                        realData.Hun_PeiBiPecent = Convert.ToSingle(obj["PeibiPercent"]);
                        realData.Hun_OutPress = Convert.ToSingle(obj["Outpress"]);
     
                        var hunhe_co2 = NetFactory.redisPool.ReadKey("MTCD:STAT:FristHunheqi_CO2");
                        if (hunhe_co2.IsSuccess)
                        {
                            realData.Hun_CO2_Custom = (float)(ServerInfo.CO2Setting - (actruelly - Convert.ToSingle(hunhe_co2.Content)) * (realData.Hun_PeiBiPecent / 100.0f) * 1.997);
                        }

                        var hunhe = NetFactory.redisPool.ReadKey("MTCD:STAT:FristHunheqi_AR");
                        if (hunhe.IsSuccess)
                        {
                            realData.Hun_AR_Custom = (float)(ServerInfo.ARSetting - (actruelly - Convert.ToSingle(hunhe.Content)) * (1 - realData.Hun_PeiBiPecent / 100.0f) * 1.784);
                        }


                        JArray array = JArray.Parse(obj["Alarm"].ToString());
                        bool[] alarm = array.ToObject<bool[]>();
                        for (int i = 0; i < alarm.Length; i++)
                        {
                            if (alarm[0])
                                realData.Hun_Status2 = true;
                            if (alarm[1])
                                realData.Hun_Status3 = true;
                        }
                    }
                }

                var res4 = NetFactory.redisPool.ReadHashKey("MTCD:ConstGateWay", DeviceID.天然气站.ToString());
                if (res4.IsSuccess)
                {
                    JObject obj = JObject.Parse(res4.Content);
                    if (obj.Value<bool>("IsActive"))
                    {
                        realData.Tian_Press = Convert.ToSingle(obj["Pressure"]);
                        float TotalVolume = Convert.ToSingle(obj["TotalVolume"]);
                        var gas = NetFactory.redisPool.ReadKey("MTCD:STAT:FristTianranqi");
                        if (gas.IsSuccess)
                        {
                            realData.Tian_Custom = ServerInfo.GasSetting - (TotalVolume - Convert.ToSingle(gas.Content));
                        }
                    }
                    JArray array = JArray.Parse(obj["Alarm"].ToString());
                    bool[] alarm = array.ToObject<bool[]>();
                    for (int i = 0; i < alarm.Length; i++)
                    {
                        if (alarm[i])
                            realData.Hun_Status1 = true;
                    }
                }

                return MessageModel.CreateSuccessResult(realData);
            }
            catch (Exception ex)
            {
                return new MessageModel(ex.Message);
            }
              
            
        }

        /// <summary>
        /// 获取一周能耗
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public MessageModel GetOneWeekEnergy()
        {
            MysqlHelper mysql = new MysqlHelper();
            string sql1 = $"select CreatTime,SUM(ActualEnergy),ChanLiang from stat_alldeviceenergy where DeviceId!={DeviceID.高配房} and DeviceId!={DeviceID.天然气站} and CreatTime>'{DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd HH:mm:ss")}' and CreatTime<'{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}' GROUP BY CreatTime";
            DataTable dt1 = mysql.ExecuteQuery(sql1);

            EnergyHistroyChart energy = new EnergyHistroyChart();

            if (dt1 != null && dt1.Rows.Count > 0)
            {
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    energy.XTime.Add(Convert.ToDateTime(dt1.Rows[i][0]).ToString("MM.dd"));

                    float actruelenergy = Convert.ToSingle(dt1.Rows[i][1]);
                    float outValue ;
                    float savingValue;
                    int chanliang  = Convert.ToInt32(dt1.Rows[i][2]);
                    if (ServerInfo.EnnrgyStandard * chanliang - actruelenergy > 0)
                    {
                        savingValue = ServerInfo.EnnrgyStandard * chanliang - actruelenergy;
                        outValue = 0;
                    }
                    else
                    {
                        outValue = ServerInfo.EnnrgyStandard * chanliang - actruelenergy;
                        savingValue = 0;
                    }

                    energy.Actruely.Add(actruelenergy);
                    energy.OutValue.Add(outValue);
                    energy.SavingValue.Add(savingValue);                  
                }
                return MessageModel.CreateSuccessResult(energy);
            }

            return new MessageModel("未查询到数据");

        }



        /// <summary>
        /// 获取一周天然气消耗
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public MessageModel GetOneWeekGas()
        {
            //int[] alldevice = new int[] { DeviceID.等离子 , DeviceID.埋弧焊, DeviceID.预拱, DeviceID.自动焊, DeviceID.剪折线, DeviceID.机器人, DeviceID.抛丸机, DeviceID.油漆线,
            //DeviceID.组焊,DeviceID.空压机,DeviceID.北跨20T行车,DeviceID.北跨20T行车};
            MysqlHelper mysql = new MysqlHelper();
            string sql1 = $"select * from stat_alldeviceenergy where DeviceId={DeviceID.天然气站} and CreatTime>'{DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd HH:mm:ss")}' and CreatTime<'{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}'";
            DataTable dt1 = mysql.ExecuteQuery(sql1);

            EnergyHistroyChart energy = new EnergyHistroyChart();

            if (dt1 != null && dt1.Rows.Count > 0)
            {
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    energy.XTime.Add(Convert.ToDateTime(dt1.Rows[i]["CreatTime"]).ToString("MM.dd"));

                    float actruelenergy = Convert.ToSingle(dt1.Rows[i]["ActualEnergy"]);
                    float outValue = Convert.ToSingle(dt1.Rows[i]["OutValue"]);
                    float savingValue = Convert.ToSingle(dt1.Rows[i]["SavingValue"]);

                    energy.Actruely.Add(actruelenergy);
                    energy.OutValue.Add(outValue);
                    energy.SavingValue.Add(savingValue);
                }
                return MessageModel.CreateSuccessResult(energy);
            }

            return new MessageModel("未查询到数据");
        }



        ///// <summary>
        ///// 获取实时能耗曲线图
        ///// </summary>
        ///// <returns></returns>
        //[HttpGet]
        //public MessageModel GetRealChart()
        //{
        //    EnnergyChartReal data = new EnnergyChartReal();
        //    //int[] alldevice = new int[] { DeviceID.等离子 , DeviceID.埋弧焊, DeviceID.预拱, DeviceID.自动焊, DeviceID.剪折线, DeviceID.机器人, DeviceID.抛丸机, DeviceID.油漆线,
        //    //DeviceID.组焊,};
        //    try
        //    {
        //        data.Standardenergy = ServerInfo.AllUnitEnnrgyStandard;// 标准单节能耗
        //        //等离子
        //        var denglizi1 = NetFactory.redisPool.ReadHashKey("MTCD:AllDeviceEnnrgy", DeviceID.等离子.ToString());
        //        if (denglizi1.IsSuccess)
        //        {
        //            JObject obj = JObject.Parse(denglizi1.Content);
        //            data.Actruelenergy[0] = obj.Value<float>("ActualEnergy"); //能耗
        //        }
        //        else
        //        {
        //            return new MessageModel(denglizi1.Message);
        //        }
        //        var denglizi2 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.等离子.ToString());
        //        if (denglizi2.IsSuccess)
        //        {
        //            JObject obj = JObject.Parse(denglizi2.Content);
        //            if (obj.Value<bool>("IsActive"))
        //            {
        //                var ServerInfo.YouQiChanLiang = Convert.ToInt32(obj["DailyOutput"]); //产量
        //                //单节实际标准
        //               data.Unitacenergy[0] =data.Actruelenergy[0] / ServerInfo.YouQiChanLiang;
        //            }
        //        }


        //        // 埋弧焊
        //        var maihuhan1 = NetFactory.redisPool.ReadHashKey("MTCD:AllDeviceEnnrgy", DeviceID.埋弧焊.ToString());
        //        if (maihuhan1.IsSuccess)
        //        {
        //            JObject obj = JObject.Parse(maihuhan1.Content);
        //            data.Actruelenergy[1] = obj.Value<float>("ActualEnergy"); //能耗
        //        }
        //        var maihuhan2 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.埋弧焊.ToString());
        //        if (maihuhan2.IsSuccess)
        //        {
        //            JObject obj = JObject.Parse(maihuhan2.Content);
        //            if (obj.Value<bool>("IsActive"))
        //            {
        //                var ServerInfo.YouQiChanLiang = Convert.ToInt32(obj["DailyOutput"]); //产量
        //                data.Unitacenergy[1] = data.Actruelenergy[1] / ServerInfo.YouQiChanLiang;
        //            }
        //        }



        //        // 预拱
        //        //var yugong1 = NetFactory.redisPool.ReadHashKey("MTCD:AllDeviceEnnrgy", DeviceID.预拱.ToString());
        //        //if (yugong1.IsSuccess)
        //        //{
        //        //    JObject obj = JObject.Parse(yugong1.Content);
        //        //    data.Actruelenergy[2] = obj.Value<float>("ActualEnergy"); //能耗
        //        //}
        //        var yugong2 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.预拱.ToString());
        //        if (yugong2.IsSuccess)
        //        {
        //            JObject obj = JObject.Parse(yugong2.Content);
        //            if (obj.Value<bool>("IsActive"))
        //            {
        //                var ServerInfo.YouQiChanLiang = Convert.ToInt32(obj["DailyOutput"]); //产量
        //                data.Unitacenergy[2] = data.Actruelenergy[2] / ServerInfo.YouQiChanLiang;
        //            }
        //        }



        //        // 自动焊
        //        var zidonghan1 = NetFactory.redisPool.ReadHashKey("MTCD:AllDeviceEnnrgy", DeviceID.自动焊.ToString());
        //        if (zidonghan1.IsSuccess)
        //        {
        //            JObject obj = JObject.Parse(zidonghan1.Content);
        //            data.Actruelenergy[2] = obj.Value<float>("ActualEnergy") * 0.05f;  //预拱
        //            data.Actruelenergy[3] = obj.Value<float>("ActualEnergy") * 0.95f; //自动焊能耗
        //        }
        //        var zidonghan2 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.自动焊.ToString());
        //        if (zidonghan2.IsSuccess)
        //        {
        //            JObject obj = JObject.Parse(zidonghan2.Content);
        //            if (obj.Value<bool>("IsActive"))
        //            {
        //                var ServerInfo.YouQiChanLiang = Convert.ToInt32(obj["DailyOutput"]); //产量
        //                data.Unitacenergy[3] = data.Actruelenergy[3] / ServerInfo.YouQiChanLiang;
        //            }
        //        }



        //        // 剪折线
        //        var jianzhexian1 = NetFactory.redisPool.ReadHashKey("MTCD:AllDeviceEnnrgy", DeviceID.剪折线.ToString());
        //        if (jianzhexian1.IsSuccess)
        //        {
        //            JObject obj = JObject.Parse(jianzhexian1.Content);
        //            data.Actruelenergy[4] = obj.Value<float>("ActualEnergy"); //能耗
        //        }
        //        var jianzhexian2 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.剪折线.ToString());
        //        if (jianzhexian2.IsSuccess)
        //        {
        //            JObject obj = JObject.Parse(jianzhexian2.Content);
        //            if (obj.Value<bool>("IsActive"))
        //            {
        //                var ServerInfo.YouQiChanLiang = Convert.ToInt32(obj["DailyOutput"]); //产量
        //                data.Unitacenergy[4] = data.Actruelenergy[4] / shengchan;
        //            }
        //        }



        //        // 机器人
        //        var jiqiren1 = NetFactory.redisPool.ReadHashKey("MTCD:AllDeviceEnnrgy", DeviceID.机器人.ToString());
        //        if (jiqiren1.IsSuccess)
        //        {
        //            JObject obj = JObject.Parse(jiqiren1.Content);
        //            data.Actruelenergy[5] = obj.Value<float>("ActualEnergy"); //能耗
        //        }
        //        var jiqiren2 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.机器人.ToString());
        //        if (jiqiren2.IsSuccess)
        //        {
        //            JObject obj = JObject.Parse(jiqiren2.Content);
        //            if (obj.Value<bool>("IsActive"))
        //            {
        //                var shengchan = Convert.ToInt32(obj["DailyOutput"]); //产量
        //                data.Unitacenergy[5] = data.Actruelenergy[5] / shengchan;
        //            }
        //        }


        //        // 抛丸机
        //        var paowanji1 = NetFactory.redisPool.ReadHashKey("MTCD:AllDeviceEnnrgy", DeviceID.抛丸机.ToString());
        //        if (paowanji1.IsSuccess)
        //        {
        //            JObject obj = JObject.Parse(paowanji1.Content);
        //            data.Actruelenergy[6] = obj.Value<float>("ActualEnergy"); //能耗
        //        }
        //        var paowanji2 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.抛丸机.ToString());
        //        if (paowanji2.IsSuccess)
        //        {
        //            JObject obj = JObject.Parse(paowanji2.Content);
        //            if (obj.Value<bool>("IsActive"))
        //            {
        //                var shengchan = Convert.ToInt32(obj["DailyOutput"]); //产量
        //                data.Unitacenergy[6] = data.Actruelenergy[6] / shengchan;
        //            }
        //        }


        //        //油漆线
        //        var youqi1 = NetFactory.redisPool.ReadHashKey("MTCD:AllDeviceEnnrgy", DeviceID.油漆线.ToString());
        //        if (youqi1.IsSuccess)
        //        {
        //            JObject obj = JObject.Parse(youqi1.Content);
        //            data.Actruelenergy[7] = obj.Value<float>("ActualEnergy"); //能耗
        //        }
        //        var youqi2 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.油漆线.ToString());
        //        if (youqi2.IsSuccess)
        //        {
        //            JObject obj = JObject.Parse(youqi2.Content);
        //            if (obj.Value<bool>("IsActive"))
        //            {
        //                var shengchan = Convert.ToInt32(obj["DailyOutput"]); //产量
        //                data.Unitacenergy[7] = data.Actruelenergy[7] / shengchan;
        //            }
        //        }



        //        //组焊 
        //        var zuhan1 = NetFactory.redisPool.ReadHashKey("MTCD:AllDeviceEnnrgy", DeviceID.组焊.ToString());
        //        if (zuhan1.IsSuccess)
        //        {
        //            JObject obj = JObject.Parse(denglizi1.Content);
        //            data.Actruelenergy[8] = obj.Value<float>("ActualEnergy"); //能耗 AP13+14+15
        //            data.Actruelenergy[9] = obj.Value<float>("FanZhuanActualEnergy"); //翻转 能耗
        //        }
        //        var zuhan2 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.组焊.ToString());
        //        if (zuhan2.IsSuccess)
        //        {
        //            JObject obj = JObject.Parse(zuhan2.Content);
        //            if (obj.Value<bool>("IsActive"))
        //            {
        //                var shengchan = Convert.ToInt32(obj["DailyOutput"]); //产量
        //                data.Unitacenergy[8] = data.Actruelenergy[8] / shengchan;
        //                data.Unitacenergy[9] = data.Actruelenergy[9] / shengchan;
        //            }
        //        }
        //        return MessageModel.CreateSuccessResult(data);
        //    }
        //    catch (Exception ex)
        //    {

        //        return new MessageModel(ex.Message);
        //    }

        //}



        /// <summary>
        /// 获取实时能耗曲线图
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public MessageModel<EnnergyChartReal> GetRealChart()
        {
            EnnergyChartReal data = new EnnergyChartReal();
     

            try
            {
                data.Standardenergy = ServerInfo.AllUnitEnnrgyStandard;// 标准单节能耗

                //等离子
                var denglizi1 = NetFactory.redisPool.ReadHashKey("MTCD:AllDeviceEnnrgy", DeviceID.等离子.ToString());
                if (denglizi1.IsSuccess)
                {
                    JObject obj = JObject.Parse(denglizi1.Content);
                    data.Actruelenergy[0] = obj.Value<float>("ActualEnergy"); //能耗
                    if (ServerInfo.YouQiChanLiang != 0)
                        data.Unitacenergy[0] = data.Actruelenergy[0] / ServerInfo.YouQiChanLiang;
                }



                // 埋弧焊
                var maihuhan1 = NetFactory.redisPool.ReadHashKey("MTCD:AllDeviceEnnrgy", DeviceID.埋弧焊.ToString());
                if (maihuhan1.IsSuccess)
                {
                    JObject obj = JObject.Parse(maihuhan1.Content);
                    data.Actruelenergy[1] = obj.Value<float>("ActualEnergy"); //能耗
                    if (ServerInfo.YouQiChanLiang != 0)
                        data.Unitacenergy[1] = data.Actruelenergy[1] / ServerInfo.YouQiChanLiang;
                }

                // 预拱
                //var yugong1 = NetFactory.redisPool.ReadHashKey("MTCD:AllDeviceEnnrgy", DeviceID.预拱.ToString());
                //if (yugong1.IsSuccess)
                //{
                //    JObject obj = JObject.Parse(yugong1.Content);
                //    data.Actruelenergy[2] = obj.Value<float>("ActualEnergy"); //能耗
                //}



                // 自动焊
                var zidonghan1 = NetFactory.redisPool.ReadHashKey("MTCD:AllDeviceEnnrgy", DeviceID.自动焊.ToString());
                if (zidonghan1.IsSuccess)
                {
                    JObject obj = JObject.Parse(zidonghan1.Content);
                    data.Actruelenergy[2] = obj.Value<float>("ActualEnergy") * 0.05f;  //预拱
                    data.Actruelenergy[3] = obj.Value<float>("ActualEnergy") * 0.95f; //自动焊能耗
                    if (ServerInfo.YouQiChanLiang != 0)
                        data.Unitacenergy[2] = data.Actruelenergy[2] / ServerInfo.YouQiChanLiang;
                    if (ServerInfo.YouQiChanLiang != 0)
                        data.Unitacenergy[3] = data.Actruelenergy[3] / ServerInfo.YouQiChanLiang;
                }



                // 剪折线
                var jianzhexian1 = NetFactory.redisPool.ReadHashKey("MTCD:AllDeviceEnnrgy", DeviceID.剪折线.ToString());
                if (jianzhexian1.IsSuccess)
                {
                    JObject obj = JObject.Parse(jianzhexian1.Content);
                    data.Actruelenergy[4] = obj.Value<float>("ActualEnergy"); //能耗
                    if (ServerInfo.YouQiChanLiang != 0)
                        data.Unitacenergy[4] = data.Actruelenergy[4] / ServerInfo.YouQiChanLiang;
                }



                // 机器人
                var jiqiren1 = NetFactory.redisPool.ReadHashKey("MTCD:AllDeviceEnnrgy", DeviceID.机器人.ToString());
                if (jiqiren1.IsSuccess)
                {
                    JObject obj = JObject.Parse(jiqiren1.Content);
                    data.Actruelenergy[5] = obj.Value<float>("ActualEnergy"); //能耗
                    if (ServerInfo.YouQiChanLiang != 0)
                        data.Unitacenergy[5] = data.Actruelenergy[5] / ServerInfo.YouQiChanLiang;
                }


                // 抛丸机
                var paowanji1 = NetFactory.redisPool.ReadHashKey("MTCD:AllDeviceEnnrgy", DeviceID.抛丸机.ToString());
                if (paowanji1.IsSuccess)
                {
                    JObject obj = JObject.Parse(paowanji1.Content);
                    data.Actruelenergy[6] = obj.Value<float>("ActualEnergy"); //能耗
                    if (ServerInfo.YouQiChanLiang != 0)
                        data.Unitacenergy[6] = data.Actruelenergy[6] / ServerInfo.YouQiChanLiang;
                }



                //油漆线
                var youqi1 = NetFactory.redisPool.ReadHashKey("MTCD:AllDeviceEnnrgy", DeviceID.油漆线.ToString());
                if (youqi1.IsSuccess)
                {
                    JObject obj = JObject.Parse(youqi1.Content);
                    data.Actruelenergy[7] = obj.Value<float>("ActualEnergy"); //能耗
                    if (ServerInfo.YouQiChanLiang != 0)
                        data.Unitacenergy[7] = data.Actruelenergy[7] / ServerInfo.YouQiChanLiang;
                }



                //组焊 
                var zuhan1 = NetFactory.redisPool.ReadHashKey("MTCD:AllDeviceEnnrgy", DeviceID.组焊.ToString());
                if (zuhan1.IsSuccess)
                {
                    JObject obj = JObject.Parse(zuhan1.Content);
                    data.Actruelenergy[8] = obj.Value<float>("ActualEnergy"); //能耗 AP13+14+15
                    data.Actruelenergy[9] = obj.Value<float>("FanZhuanActualEnergy"); //翻转 能耗
                    if (ServerInfo.YouQiChanLiang != 0)
                        data.Unitacenergy[8] = data.Actruelenergy[8] / ServerInfo.YouQiChanLiang;
                    if (ServerInfo.YouQiChanLiang != 0)
                        data.Unitacenergy[9] = data.Actruelenergy[9] / ServerInfo.YouQiChanLiang;
                }

                //EnnergyChartReal1 data1 = new EnnergyChartReal1();
                //data1.Actruelenergy = data.Actruelenergy.ToList();
                //data1.Standardenergy = data.Standardenergy.ToList();
                //data1.Unitacenergy = data.Unitacenergy.ToList();
                //Console.WriteLine(data);
                return MessageModel.CreateSuccessResult(data);
            }
            catch (Exception ex)
            {

                return MessageModel.CreateFailedResult<EnnergyChartReal>(ex.Message);
            }

        }

        /// <summary>
        /// 获取实时通道数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public MessageModel GetXingCheData()
        {
            XingCheDataListVM model = new XingCheDataListVM();
            var res = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.南跨20T行车.ToString());
            if (res.IsSuccess)
            {
                JObject obj = JObject.Parse(res.Content);
                if (obj.Value<bool>("IsActive"))
                {
                    model.nk20OnLine = true;
                    JArray array = JArray.Parse(obj["PassData"].ToString());
                    for (int i = 0; i < array.Count; i++)
                    {
                        var list = array[i].Children().ToList();
                        var p0 = list[0].ToObject<JProperty>();
                        var p1 = list[1].ToObject<JProperty>();
                        var p2 = list[2].ToObject<JProperty>();
                        var p3 = list[3].ToObject<JProperty>();
                        model.nk20List.Add(new XingCheDataVM
                        {
                            PassName1 = p0.Name,
                            Value1 = p0.Value.ToString(),
                            lowLimitData1 = p1.Value.ToString(),
                            upLimitData1 = p2.Value.ToString(),
                            Unit1 = p3.Value.ToString(),
                        });
                    }
                }
            }


            var res1 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.北跨20T行车.ToString());
            if (res1.IsSuccess)
            {
                JObject obj = JObject.Parse(res1.Content);
                if (obj.Value<bool>("IsActive"))
                {
                    model.bk20OnLine = true;
                    JArray array = JArray.Parse(obj["PassData"].ToString());
                    for (int i = 0; i < array.Count; i++)
                    {
                        var list = array[i].Children().ToList();
                        var p0 = list[0].ToObject<JProperty>();
                        var p1 = list[1].ToObject<JProperty>();
                        var p2 = list[2].ToObject<JProperty>();
                        var p3 = list[3].ToObject<JProperty>();
                        model.bk20List.Add(new XingCheDataVM
                        {
                            PassName1 = p0.Name,
                            Value1 = p0.Value.ToString(),
                            lowLimitData1 = p1.Value.ToString(),
                            upLimitData1 = p2.Value.ToString(),
                            Unit1 = p3.Value.ToString(),
                        });
                    }
                }
            }


            var res2 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.等离子行车.ToString());
            if (res2.IsSuccess)
            {
                JObject obj = JObject.Parse(res2.Content);
                if (obj.Value<bool>("IsActive"))
                {
                    model.dlzxcOnLine = true;
                    JArray array = JArray.Parse(obj["PassData"].ToString());
                    for (int i = 0; i < array.Count; i++)
                    {
                        var list = array[i].Children().ToList();
                        var p0 = list[0].ToObject<JProperty>();
                        var p1 = list[1].ToObject<JProperty>();
                        var p2 = list[2].ToObject<JProperty>();
                        var p3 = list[3].ToObject<JProperty>();
                        model.dlzxcList.Add(new XingCheDataVM
                        {
                            PassName1 = p0.Name,
                            Value1 = p0.Value.ToString(),
                            lowLimitData1 = p1.Value.ToString(),
                            upLimitData1 = p2.Value.ToString(),
                            Unit1 = p3.Value.ToString(),
                        });
                    }
                }
            }


            var res3 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.剪折半门吊.ToString());
            if (res3.IsSuccess)
            {
                JObject obj = JObject.Parse(res3.Content);
                if (obj.Value<bool>("IsActive"))
                {
                    model.jzbmdOnLine = true;
                    JArray array = JArray.Parse(obj["PassData"].ToString());
                    for (int i = 0; i < array.Count; i++)
                    {
                        var list = array[i].Children().ToList();
                        var p0 = list[0].ToObject<JProperty>();
                        var p1 = list[1].ToObject<JProperty>();
                        var p2 = list[2].ToObject<JProperty>();
                        var p3 = list[3].ToObject<JProperty>();
                        model.jzbmdList.Add(new XingCheDataVM
                        {
                            PassName1 = p0.Name,
                            Value1 = p0.Value.ToString(),
                            lowLimitData1 = p1.Value.ToString(),
                            upLimitData1 = p2.Value.ToString(),
                            Unit1 = p3.Value.ToString(),
                        });
                    }
                }
            }


            var res4 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.组焊半门吊.ToString());
            if (res4.IsSuccess)
            {
                JObject obj = JObject.Parse(res4.Content);
                if (obj.Value<bool>("IsActive"))
                {
                    model.zhbmdOnLine = true;
                    JArray array = JArray.Parse(obj["PassData"].ToString());
                    for (int i = 0; i < array.Count; i++)
                    {
                        var list = array[i].Children().ToList();
                        var p0 = list[0].ToObject<JProperty>();
                        var p1 = list[1].ToObject<JProperty>();
                        var p2 = list[2].ToObject<JProperty>();
                        var p3 = list[3].ToObject<JProperty>();
                        model.zhbmdList.Add(new XingCheDataVM
                        {
                            PassName1 = p0.Name,
                            Value1 = p0.Value.ToString(),
                            lowLimitData1 = p1.Value.ToString(),
                            upLimitData1 = p2.Value.ToString(),
                            Unit1 = p3.Value.ToString(),
                        });
                    }
                }
            }


            var res5 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.机器人半门吊.ToString());
            if (res5.IsSuccess)
            {
                JObject obj = JObject.Parse(res5.Content);
                if (obj.Value<bool>("IsActive"))
                {
                    model.jqrbmdOnLine = true;
                    JArray array = JArray.Parse(obj["PassData"].ToString());
                    for (int i = 0; i < array.Count; i++)
                    {
                        var list = array[i].Children().ToList();
                        var p0 = list[0].ToObject<JProperty>();
                        var p1 = list[1].ToObject<JProperty>();
                        var p2 = list[2].ToObject<JProperty>();
                        var p3 = list[3].ToObject<JProperty>();
                        model.jqrbmdList.Add(new XingCheDataVM
                        {
                            PassName1 = p0.Name,
                            Value1 = p0.Value.ToString(),
                            lowLimitData1 = p1.Value.ToString(),
                            upLimitData1 = p2.Value.ToString(),
                            Unit1 = p3.Value.ToString(),
                        });
                    }
                }
            }


            var res6 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.抛丸机上线行车.ToString());
            if (res6.IsSuccess)
            {
                JObject obj = JObject.Parse(res6.Content);
                if (obj.Value<bool>("IsActive"))
                {
                    model.pwjsxxcOnLine = true;
                    JArray array = JArray.Parse(obj["PassData"].ToString());
                    for (int i = 0; i < array.Count; i++)
                    {
                        var list = array[i].Children().ToList();
                        var p0 = list[0].ToObject<JProperty>();
                        var p1 = list[1].ToObject<JProperty>();
                        var p2 = list[2].ToObject<JProperty>();
                        var p3 = list[3].ToObject<JProperty>();
                        model.pwjsxxcList.Add(new XingCheDataVM
                        {
                            PassName1 = p0.Name,
                            Value1 = p0.Value.ToString(),
                            lowLimitData1 = p1.Value.ToString(),
                            upLimitData1 = p2.Value.ToString(),
                            Unit1 = p3.Value.ToString(),
                        });
                    }
                }
            }



            var res7 = NetFactory.redisPool.ReadHashKey("MTCD:PassGateWay", DeviceID.油漆上线.ToString());
            if (res7.IsSuccess)
            {
                JObject obj = JObject.Parse(res7.Content);
                if (obj.Value<bool>("IsActive"))
                {
                    model.yqsxOnLine = true;
                    JArray array = JArray.Parse(obj["PassData"].ToString());
                    for (int i = 0; i < array.Count; i++)
                    {
                        var list = array[i].Children().ToList();
                        var p0 = list[0].ToObject<JProperty>();
                        var p1 = list[1].ToObject<JProperty>();
                        var p2 = list[2].ToObject<JProperty>();
                        var p3 = list[3].ToObject<JProperty>();
                        model.yqsxList.Add(new XingCheDataVM
                        {
                            PassName1 = p0.Name,
                            Value1 = p0.Value.ToString(),
                            lowLimitData1 = p1.Value.ToString(),
                            upLimitData1 = p2.Value.ToString(),
                            Unit1 = p3.Value.ToString(),
                        });
                    }
                }
            }


            return MessageModel.CreateSuccessResult(model);
        }



        /// <summary>
        /// 获取一年(12个月)维护数据图
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public MessageModel<WHOneYearDataVM> GetOneYearWHData()
        {
            //int[] alldevice = new int[] { DeviceID.等离子 , DeviceID.埋弧焊, DeviceID.预拱, DeviceID.自动焊, DeviceID.剪折线, DeviceID.机器人, DeviceID.抛丸机, DeviceID.油漆线,
            //DeviceID.组焊,DeviceID.空压机,DeviceID.北跨20T行车,DeviceID.北跨20T行车};
            MysqlHelper mysql = new MysqlHelper();
            string sql1 = $"select * from wh_tasks_data where  CreatTime>'{Convert.ToDateTime($"{DateTime.Now.Year}-1-1 00:00:00")}' and CreatTime<'{Convert.ToDateTime($"{DateTime.Now.Year}-12-31 23:59:59")}'";
            DataTable dt1 = mysql.ExecuteQuery(sql1);

            WHOneYearDataVM data = new WHOneYearDataVM();

            if (dt1 != null && dt1.Rows.Count > 0)
            {
                for (int i = 0; i < dt1.Rows.Count; i++)
                {
                    if (Convert.ToDateTime(dt1.Rows[i]["CreatTime"]).Month == 1)
                    {
                        data.Total[0]++; // 总量++
                        if (Convert.ToInt32(dt1.Rows[i]["Flag"]) == 2)
                            data.Finish[0]++;
                    }
                    else if (Convert.ToDateTime(dt1.Rows[i]["CreatTime"]).Month == 2)
                    {
                        data.Total[1]++; // 总量++
                        if (Convert.ToInt32(dt1.Rows[i]["Flag"]) == 2)
                            data.Finish[1]++;
                    }
                    else if (Convert.ToDateTime(dt1.Rows[i]["CreatTime"]).Month == 3)
                    {
                        data.Total[2]++; // 总量++
                        if (Convert.ToInt32(dt1.Rows[i]["Flag"]) == 2)
                            data.Finish[2]++;
                    }
                    else if (Convert.ToDateTime(dt1.Rows[i]["CreatTime"]).Month == 4)
                    {
                        data.Total[3]++; // 总量++
                        if (Convert.ToInt32(dt1.Rows[i]["Flag"]) == 2)
                            data.Finish[3]++;
                    }
                    else if (Convert.ToDateTime(dt1.Rows[i]["CreatTime"]).Month == 5)
                    {
                        data.Total[4]++; // 总量++
                        if (Convert.ToInt32(dt1.Rows[i]["Flag"]) == 2)
                            data.Finish[4]++;
                    }
                    else if (Convert.ToDateTime(dt1.Rows[i]["CreatTime"]).Month == 6)
                    {
                        data.Total[5]++; // 总量++
                        if (Convert.ToInt32(dt1.Rows[i]["Flag"]) == 2)
                            data.Finish[5]++;
                    }
                    else if (Convert.ToDateTime(dt1.Rows[i]["CreatTime"]).Month == 7)
                    {
                        data.Total[6]++; // 总量++
                        if (Convert.ToInt32(dt1.Rows[i]["Flag"]) == 2)
                            data.Finish[6]++;
                    }
                    else if (Convert.ToDateTime(dt1.Rows[i]["CreatTime"]).Month == 8)
                    {
                        data.Total[7]++; // 总量++
                        if (Convert.ToInt32(dt1.Rows[i]["Flag"]) == 2)
                            data.Finish[7]++;
                    }
                    else if (Convert.ToDateTime(dt1.Rows[i]["CreatTime"]).Month == 9)
                    {
                        data.Total[8]++; // 总量++
                        if (Convert.ToInt32(dt1.Rows[i]["Flag"]) == 2)
                            data.Finish[8]++;
                    }
                    else if (Convert.ToDateTime(dt1.Rows[i]["CreatTime"]).Month == 10)
                    {
                        data.Total[9]++; // 总量++
                        if (Convert.ToInt32(dt1.Rows[i]["Flag"]) == 2)
                            data.Finish[9]++;
                    }
                    else if (Convert.ToDateTime(dt1.Rows[i]["CreatTime"]).Month == 11)
                    {
                        data.Total[10]++; // 总量++
                        if (Convert.ToInt32(dt1.Rows[i]["Flag"]) == 2)
                            data.Finish[10]++;
                    }
                    else if (Convert.ToDateTime(dt1.Rows[i]["CreatTime"]).Month == 12)
                    {
                        data.Total[11]++; // 总量++
                        if (Convert.ToInt32(dt1.Rows[i]["Flag"]) == 2)
                            data.Finish[11]++;
                    }

                }
                return MessageModel.CreateSuccessResult(data);
            }

            return new MessageModel<WHOneYearDataVM>();
        }
    }
}
